package com.finger.gulimall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.finger.gulimall.ware.entity.vo.DoneVO;
import com.finger.gulimall.ware.entity.vo.MargeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.finger.gulimall.ware.entity.PurchaseEntity;
import com.finger.gulimall.ware.service.PurchaseService;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.R;


/**
 * 采购信息
 *
 * @author finger
 * @email 909128695@qq.com
 * @date 2022-03-16 18:27:01
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("ware:purchase:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 合并采购需求
     *
     * @param margeVO
     * @return
     */
    @PostMapping("/merge")
    public R marge(@RequestBody MargeVO margeVO) {

        // 合并采购需求
        purchaseService.margePurchase(margeVO);

        return R.ok();
    }

    /*
        /ware/purchase/received
        领取采购单
        [1,2,3,4]//采购单id
     */
    @PostMapping("/received")
    public R receivedPurchase(@RequestBody List<Long> ids) {

        if (ids == null || ids.size() == 0) {
            return R.error();
        }
        // 领取采购单
        purchaseService.receivedPurchase(ids);
        return R.ok();
    }

    @GetMapping("/unreceive/list")
    public R unreceiveList() {
        // 获取未分配的购物单
        PageUtils page = purchaseService.unreceiveList();

        return R.ok().put("page", page);
    }


    // /ware/purchase/done
    @PostMapping("/done")
    public R donePurchase(@RequestBody DoneVO doneVo) {
        // 采购单p
        purchaseService.done(doneVo);

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
